package com.zwf.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zwf.constant.MessageConstant;
import com.zwf.constant.StatusConstant;
import com.zwf.dto.DishDTO;
import com.zwf.dto.DishPageQueryDTO;
import com.zwf.entity.Dish;
import com.zwf.entity.DishFlavor;
import com.zwf.exception.DeletionNotAllowedException;
import com.zwf.mapper.DishFlavorMapper;
import com.zwf.mapper.DishMapper;
import com.zwf.mapper.SetmealDishMapper;
import com.zwf.result.PageResult;
import com.zwf.service.DishService;
import com.zwf.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author zwf
 * @date 2024/2/2 17:25
 */

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;

    @Autowired
    DishFlavorMapper dishFlavorMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Override
    @Transactional //保证方法的原子性
    public void add(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish); //dishDTO have,but dish not have,enable
        dishMapper.add(dish);

        //获取insert语句生成的主键值
        Long dishId=dish.getId();

        List<DishFlavor> list=dishDTO.getFlavors();
        if(list!=null && list.size()!=0){
            list.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.add(list);
        }
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page=dishMapper.pageQuery(dishPageQueryDTO);
        PageResult pageResult=new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;
    }

    /**
     * 根据id查询菜品及其关联的口味
     * @param id
     * @return
     */
    @Override
    public DishVO queryByIdWithFlavor(Long id) {
        Dish dish=dishMapper.queryById(id);
        List<DishFlavor> list=dishFlavorMapper.queryByDishId(id);
        DishVO dishVO=new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(list);
        return dishVO;
    }

    /**
     * 根据分类id查询菜品
     * @param id
     * @return
     */
    @Override
    public List<Dish> queryByCategoryId(Long id) {
        List<Dish> list=dishMapper.queryByCategoryId(id);
        return list;
    }

    /**
     * 菜品起售、停售
     *
     * @param status
     * @param id
     */
    @Override
    public void enAndDis(Integer status, Long id) {
        Dish dish=new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.update(dish);
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void modify(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

        List<DishFlavor> list=dishDTO.getFlavors();
        if(list!=null && list.size()!=0) {
            list.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
        }
        //修改口味表，实现逻辑：将之前的口味信息删除，重新添加口味信息
        dishFlavorMapper.delete(dishDTO.getId());
        dishFlavorMapper.add(list);
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        ids.forEach(id->{
            if(dishMapper.queryById(id).getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            //查询是否有与其关联的套餐
            else if (setmealDishMapper.queryByDishId(id)>0) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
            dishMapper.delete(id);

            //删除与其关联的口味数据
            dishFlavorMapper.delete(id);

        });
    }

    /**
     * 根据分类id查询菜品及其口味
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> queryByCategoryIdWithFlavor(Dish dish) {
        List<DishVO> dishVOList=new ArrayList<>();
        List<Dish> dishList = dishMapper.query(dish);
        dishList.forEach(dish1 -> {
            dishVOList.add(queryByIdWithFlavor(dish1.getId()));
        });
        return dishVOList;
    }
}
